<?php

namespace App\Console\Commands;

use App\Utils\EsUtil;
use Illuminate\Console\Command;

class ClearEsLog extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'command:ClearEsLog';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Command description';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    public function handle( )
    {
        date_default_timezone_set("Asia/Shanghai");
        go(function (){
            $time = 1695632040;
            echo date("Y-m-d H:i:s",$time) . "\n";
            $condition = [
                    ['logTime','lte',$time]
            ];
            (new EsUtil)->deleteIndex("equipment",$condition);
            (new EsUtil)->deleteIndex("role_login",$condition);
            (new EsUtil)->deleteIndex("money_change",$condition);
            (new EsUtil)->deleteIndex("role_create",$condition);
            (new EsUtil)->deleteIndex("change_scene",$condition);
            (new EsUtil)->deleteIndex("soul_synthesize",$condition);

        });

    }

    //从es中删除 , 先查询,再删除

    public function equipSynthesizeDeleteFromEs($timestamp){

        if ($timestamp==-1) $timestamp = time();//所有时间

        $res = (new EsUtil)->getPaginate("equip_synthesize",[['logTime','lt',$timestamp]],1,10000);
        $array = data_get($res,'data');

        if (count($array) == 0) return 0;
        $ids = collect($array)->pluck('id')->toArray();

        $res2 = (new EsUtil)->getPaginate("equip_synthesize_use_equip",[['pid','in',$ids]],1,10000);
        $use_equip_arr = data_get($res2,'data');
        $useEquipIds = collect($use_equip_arr)->pluck('id')->toArray();

        (new EsUtil)->deleteIndex("equip_synthesize_use_equip_attr",[['pid','in',$useEquipIds]]);
        (new EsUtil)->deleteIndex("equip_synthesize_use_material",[['pid','in',$ids]]);
        (new EsUtil)->deleteIndex("equip_synthesize_new_equip_attr",[['pid','in',$ids]]);
        (new EsUtil)->deleteIndex("equip_synthesize_use_equip",[['pid','in',$ids]]);
        (new EsUtil)->deleteIndex("equip_synthesize",[['id','in',$ids]]); //5
        return count($array);
    }

    public function equipIntensifyDeleteFromEs($timestamp)
    {
        if ($timestamp==-1) $timestamp = time();//所有时间
        $res = (new EsUtil)->getPaginate("equip_intensify",[['logTime','lt',$timestamp]],1,10000);
        $array = data_get($res,'data');
        if (count($array) == 0) return 0;
        $ids = collect($array)->pluck('id')->toArray();
        (new EsUtil)->deleteIndex("equip_intensify_use_material",[['pid','in',$ids]]);
        (new EsUtil)->deleteIndex("equip_synthesize",[['id','in',$ids]]); //5
        return count($array);
    }


}
